package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.entity.ExpertClassificationVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.digitalchina.lxnsy.constant.MSG.MSG_PRIMARY_CLASSIFICATION_ALL_FIELD;
import static com.digitalchina.lxnsy.constant.MSG.MSG_PRIMARY_CLASSIFICATION_ALL_SUCCESS;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/7/19 21:09
 * 修改备注：
 * ================================================
 */
public class ExpertInfoAreaActivity extends BaseActivity {

    @Bind(R.id.tv_expert_classification)
    TextView tvExpertClassification;
    @Bind(R.id.tv_expert_area)
    TextView tvExpertArea;
    @Bind(R.id.layout_expert_group)
    LinearLayout layoutExpertGroup;

    // 1级分类
    private List<ExpertClassificationVO> expertClassificationVOList = new ArrayList<>();
    // 2级分类
    private List<List<ExpertClassificationVO.ResearchFieldTwoStagesBean>> listList = new ArrayList<>();
    // 3级分类
    private List<ExpertClassificationVO.ResearchFieldTwoStagesBean.ResearchFieldThreeStageVosBean> threeList = new ArrayList<>();
    // 3级领域
    private List<List<ExpertClassificationVO.ResearchFieldTwoStagesBean.ResearchDirectionVosBean>> areaList = new ArrayList<>();
    // 专家分类
    private OptionsPickerView pvClassification;
    // 专家领域
    private OptionsPickerView pvArea, pvAreaView;

    // 选择器所选code
    private String pvThreeCode = "";
    private String pvAreaCode = "";
    // 最终返回的字符串
    private String expertAreaTypeCode = "";
    private String expertAreaDirectionCode = "";
    // view List
    private List<View> textViewList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_expert_info_area);
        setTitle("专业领域");
        setRightText("完成");
    }

    @Override
    public void initMember() {
        BusinessExpert.primaryClassificationAll(this, mHandler);
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG_PRIMARY_CLASSIFICATION_ALL_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG_PRIMARY_CLASSIFICATION_ALL_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        expertClassificationVOList = FastJsonUtil.getListBean(object.toString(), ExpertClassificationVO.class);

        if (expertClassificationVOList != null && expertClassificationVOList.size() > 0) {
            initClassification(expertClassificationVOList);
        }
    }

    /**
     * 分类选择器
     */
    private void initClassification(final List<ExpertClassificationVO> optList) {

        for (int i = 0; i < optList.size(); i++) {
            listList.add(optList.get(i).getResearchFieldTwoStages());
        }

        pvClassification = new OptionsPickerView.Builder(ExpertInfoAreaActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                String opt1 = optList.get(options1).getPickerViewText();
                String opt2 = listList.get(options1).get(option2).getPickerViewText();
                tvExpertClassification.setText(opt1 + "  " + opt2);
                // 清除子类
                layoutExpertGroup.removeAllViews();
                layoutExpertGroup.removeAllViewsInLayout();
                areaList.clear();
                tvExpertArea.setText("");
                pvThreeCode = "";
                pvAreaCode = "";
                expertAreaTypeCode = "";
                expertAreaDirectionCode = "";


                threeList = listList.get(options1).get(option2).getResearchFieldThreeStageVos();
                for (int i = 0; i < threeList.size(); i++) {
                    areaList.add(listList.get(options1).get(option2).getResearchDirectionVos());
                }
                initArea();
            }
        })
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvClassification.setPicker(optList, listList);
    }

    /**
     * 领域选择器
     */
    private void initArea() {
        pvArea = new OptionsPickerView.Builder(ExpertInfoAreaActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                String opt1 = threeList.get(options1).getPickerViewText();
                String opt2 = areaList.get(options1).get(option2).getPickerViewText();
                tvExpertArea.setText(opt1 + "  " + opt2);
                pvThreeCode = threeList.get(options1).getCode() + ",";
                pvAreaCode = areaList.get(options1).get(option2).getCode() + ",";
            }
        })
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();

        pvArea.setPicker(threeList, areaList);
    }


    @Override
    public void rightOnClick() {
        expertAreaTypeCode = pvThreeCode;
        expertAreaDirectionCode = pvAreaCode;
        for (int i = 0; i < textViewList.size(); i++) {
            if (!StringUtil.isStrEmpty((String) textViewList.get(i).getTag(R.id.tag_expert_type))) {
                expertAreaTypeCode += textViewList.get(i).getTag(R.id.tag_expert_type);
                expertAreaDirectionCode += textViewList.get(i).getTag(R.id.tag_expert_area);
            }
        }
        if (StringUtil.isStrEmpty(expertAreaTypeCode) || StringUtil.isStrEmpty(expertAreaDirectionCode)) {
            showToast("至少选择一个领域说明");
            return;
        }

        Intent intent = new Intent();
        intent.putExtra("type", "area");
        // 三级分类
        intent.putExtra("expertAreaTypeCode", expertAreaTypeCode.substring(0, expertAreaTypeCode.length() - 1));
        // 三级领域
        intent.putExtra("expertAreaDirectionCode", expertAreaDirectionCode.substring(0, expertAreaDirectionCode.length() - 1));
        // 二级分类字符串
        intent.putExtra("tvExpertArea", tvExpertArea.getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.rl_expert_classification, R.id.rl_expert_area, R.id.layout_expert_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_expert_classification:
                if (pvClassification != null)
                    pvClassification.show();
                break;
            case R.id.rl_expert_area:
                if (StringUtil.isStrEmpty(tvExpertClassification.getText().toString())) {
                    showToast("请先选择专家分类");
                    return;
                }
                if (pvArea != null)
                    pvArea.show();
                break;
            case R.id.layout_expert_add:
                addView();
                break;
        }
    }

    /**
     * 动态添加视图
     */
    private void addView() {
        final View view = LayoutInflater.from(this).inflate(R.layout.layout_expert_area, null);
        RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.rl_expert_area);
        final ImageView ivMin = (ImageView) view.findViewById(R.id.iv_area_min);
        final TextView tvArea = (TextView) view.findViewById(R.id.tv_expert_area);
        // 删除view
        ivMin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewList.remove(layoutExpertGroup);
                layoutExpertGroup.removeView(view);
            }
        });
        // 子view分类点击事件
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isStrEmpty(tvExpertClassification.getText().toString())) {
                    showToast("请先选择专家分类");
                    return;
                }
                if (threeList == null || areaList == null || threeList.size() <= 0 || areaList.size() <= 0) {
                    showToast("获取分类失败");
                    return;
                }
                pvAreaView = new OptionsPickerView.Builder(ExpertInfoAreaActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        String opt1 = threeList.get(options1).getPickerViewText();
                        String opt2 = areaList.get(options1).get(option2).getPickerViewText();
                        tvArea.setText(opt1 + "  " + opt2);
                        tvArea.setTag(R.id.tag_expert_type, threeList.get(options1).getCode() + ",");
                        tvArea.setTag(R.id.tag_expert_area, areaList.get(options1).get(option2).getCode() + ",");
                    }
                })
                        .setSubCalSize(16)//确定取消按钮大小
                        .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                        .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(18)//设置滚轮文字大小
                        .setSelectOptions(0)  //设置默认选中项
                        .build();

                pvAreaView.setPicker(threeList, areaList);
                pvAreaView.show();
            }
        });
        textViewList.add(tvArea);
        layoutExpertGroup.addView(view);
    }
}
